var now = getNowFormatDateTime(); 
var username = sessionStorage.getItem("name");
var userid = sessionStorage.getItem("userId");
var repairid;
var calendar = new lCalendar();
var photoUrl = [];//拍照列表
var _config ;
var dutyuserid="";
calendar.init({
	'trigger': '#repairdate',
	'type': 'datetime',
	'maxDate': '2099-12-31',
	'minDate': now
});
function ICalendarClick(){
	
}
$(document).ready(function(){
	$('#saveButton').on('click',saveInfo);
    $('#repairdate').val(now);
   // $('#username').val(username);
	$("#username").prepend(username);
	$.ajax({
			url: '../../repair_info/fillAdd'
			,type:'post'
			,dataType:'json'
			,contentType: "application/json; charset=utf-8"
			,data:JSON.stringify(parentJson)  
			,success: function(res){
				 $("#repairid").append(res.data.field.repairid);
				 repairid = res.data.field.repairid;
				 $("#repairtype").empty();
				for(var i=0;i<res.data.select.equipment.length;i++){
					 $("#repairtype").append('<option value="'+res.data.select.equipment[i].value1+'">'+res.data.select.equipment[i].key1+'</option>');
				}
		     	

			}
	});
});
function saveInfo(){
	//showloading();
	try {
		var imgStr = "";
		var sendData = {};
		var insertArray = [];
		var checkFlag = true;
		var insertJson = {};
		$('div[itemDiv]').each(function(i,obj){

			$(obj).find('textarea[jsonKey]').each(function(j,el){
				if($(el).parent().parent().parent().parent().find('span').text() == '*' && $(el).val() == ''){
					alert($(el).parent().parent().parent().parent().find('h5').text().substring(2,10)+"为必填项");
					checkFlag = false;
					return false;
				}else{
					var key = $(el).attr('jsonKey');
					insertJson[key] = $(el).val();
					key = null;
				}
			});
			$(obj).find('select[jsonKey]').each(function(j,el){
				if($(el).parent().parent().parent().parent().find('span').text() == '*' && $(el).val() == ''){
					alert($(el).parent().parent().parent().parent().find('h5').text()+"为必填项");
					checkFlag = false;
					return false;
				}else{
					var key = $(el).attr('jsonKey');
					insertJson[key] = $(el).val();
					key = null;
				}
			});
			if(checkFlag){
				$(obj).find('input[jsonKey]').each(function(j,el){
					if($(el).parent().find('span').text() == '*' && $(el).val() == ''){
						alert($(el).parent().find('h5').text().substring(2,6)+"为必填项");
						checkFlag = false;
						return false;
					}else{
						var key = $(el).attr('jsonKey');
						insertJson[key] = $(el).val();
						//console.log(insertJson);
						key = null;
					}
				}); 
			}else{
				return false;
			}

		});
		console.log(insertJson);
		insertJson.repairid = repairid;
		insertJson.pic = photoUrl.join(',');
		insertJson.lrr = userid;
		insertJson.flag = 1;
		insertJson.dutyuser = dutyuserid;
		usid = dutyuserid;
		if(checkFlag){
			$.ajax({
				url: "../../repair_info/add"
				,type:'post'
				,dataType:'json'
				,async: false
				,contentType: "application/json; charset=utf-8"
				,data:JSON.stringify(insertJson) 
				,success: function(res){
					if(res.status==1002){
						layer.msg(res.msg,{time:1000},function(){
							ding();
							history.go(-1);

						});
					}else{
						layer.msg(res.msg);
					}
				}					
			});
		}
	} catch (e) {
		console.log(e)
	} finally{
		hideloading();
	}		
}	
function ding(){
	$.ajax({
		url: '../../repair_info/remind'
		,type:'post'
		,dataType:'json'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify({"agentid":"1430279035","userid":usid,"url":"http://123.56.177.178:8383/unite_platform/mobile_page/index_weixiu?v="+Math.random(),"title":"门店报修通知","text":"报修单号"+repairid})  
		,success: function(res){
		}
	});
	layer.msg("操作成功",{time:1000},function(){
			history.go(-1);
	});
}
$('#paizhao').on('click',function(){
	openphoto();
});
//打开钉钉拍照
function openphoto(){
	if(_config == undefined){
		$.ajax({
			type : "post",
			data:JSON.stringify({"pageUrl":location.href.replace(/\\&/g,'%26')}),
			contentType: "application/json; charset=utf-8",
			async: false,
			url : "../../dingding/getconfig",
			dataType : "json",
			success : function(suc) {
//				console.log(suc);
				_config = suc.config;
			}
		}); 
		dd.config({
			agentId : _config.agentid,
			corpId : _config.corpId,
			timeStamp : _config.timeStamp,
			nonceStr : _config.nonceStr,
			signature : _config.signature,
			jsApiList : [ 'runtime.info','biz.util.uploadImage' ]
		});
	}
	dd.ready(function(){
		dd.biz.util.uploadImage({
		    compression: false,//(是否压缩，默认为true)
		    quality: 100, // 图片压缩质量, 
		    resize: 100, // 图片缩放率
		    stickers: {   // 水印信息
		        time: getNowFormatDateTime(),
		        username: sessionStorage.getItem("name")
		    },
		    onSuccess : function(result) {
			photoUrl.push(result[0]);
			$('#paizhapoUrl .clearfix').remove()
			$('#paizhapoUrl').append('<div class="wig_img">'
					+'<div class="img_box">'
					+'<img alt="100x100" src="'+result[0]+'" onclick="showAllPic(\''+result[0]+'\')"/>'
					+'<img class="close_b" src="../../genius/style/mobile_page/assets/images/close.png" onclick="deletePhoto(this)">'
					+'</div>'
					+'</div>');
			$('#paizhapoUrl').append('<div class="clearfix"></div>');
		    },
		    onFail : function(err) {}
		});
	});
}	
//删除图片
function deletePhoto(obj){
	dd.device.notification.confirm({
	    message: "删除图片吗",
	    title: "提示",
	    buttonLabels: ['是', '否'],
	    onSuccess : function(result) {
	    	if(result.buttonIndex == '0'){
	    		var index = $(obj).parent().parent().index();
	    		$(obj).parent().parent().remove();
	    		photoUrl.splice(index,1);
	    	}
	        
	    },
	    onFail : function(err) {}
	});
	 
}
//预览图片
function showAllPic(imgOne){
	dd.biz.util.previewImage({
		urls: photoUrl,//图片地址列表
		current: imgOne,//当前显示的图片链接
		onSuccess : function(result) {
			/**/
		},
		onFail : function(err) {}
	})
}
function setshopname(){
	var shopcode = $('#shopcode').val();
	$('#shopname').val("");
	$.ajax({
			url: '../../repair_info/fillshopname'
			,type:'post'
			,dataType:'json'
			,contentType: "application/json; charset=utf-8"
			,data:JSON.stringify({'shopcode':shopcode})  
			,success: function(res){
				console.log(res);
				if(res.data.field != undefined){
					$('#shopname').val(res.data.field.deptname);
					dutyuserid = res.data.field.dutyuser;
				}
				
			}
	});
}
